var vm = new Vue({
    el:"#myOrder",
    data:{
        user_id:'',
        user_name:'',
        user_image:'',
        city:'',
        web_token:'',
        lists: [],  //已收货订单列表
        orderLists: [],  //类型订单列表
        detailsList: [],  //查看物流列表
        evaluationList: [],  //评价商品列表
        comment:'',
        complaint_tel:'', // 投诉联系电话
        pageNo:1 ,
        complain_content : '' , // 投诉内容
        complain_img : [] , // 投诉上传图
        complain_tel : '' , // 投诉联系电话
        oss_url:oss_url
    },
    methods:{
        // 跳转编辑个人信息
        goMyinfo:function () {
            location.href = "/userweb/index/editMyInfo";
        },
        change:function (num) {
            if ( 4 ==num){
                $("#dqrOrder").show();
            }else{
                $("#dqrOrder").hide();
            }
            if(5==num){
                $("#dpjOrder").show();
            }else{
                $("#dpjOrder").hide();
            }
            $("#dqrOrder_check_logistics").hide();
            $("#dpjOrder_complaints").hide();
            $("#dpjOrder_evaluation").hide();
        },
        // 已收货订单
        ini: function () {
            var url = '/userweb/my/myOrder';
            var data = {
                // user_id: localStorage.getItem('user_id'),
                // token: localStorage.getItem('web_token'),
                user_id:1,
                type: 'confirmed',
                page_no:this.pageNo
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                if (res.data.status == 200) {
                    // console.log(res.data);
                    this.lists = res.data.order.list;
                } else {
                    layer.msg(res.data.msg,{
                        skin:'winning-class',
                        time:3000
                    });
                }
            }, function (res) {

            });
        },
        // 订单搜索
        search: function () {
            var starTime = $('#startTime').val();
            var endTime = $('#endTime').val();
            var url = '/userweb/my/orderSearch';
            var data ={
                user_id:1,
                starTime:starTime,
                endTime:endTime,
                page_no:this.pageNo
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                if (res.data.status == 200) {
                    // console.log(res.data);
                    this.searchLists = res.data.order.list;
                    $('.my_show_area').hide();
                    // $('.tab-content').hide();
                } else {
                    layer.msg(res.data.msg,{
                        skin:'winning-class',
                        time:3000
                    });
                }
            }, function (res) {

            });
        } ,
        // 上传投诉图片
        uploadImg : function ( e ) {
            this.loading = true;
            var data = new FormData() , _this = this , html = '' , url = '/apishop/Product/uploadImg' , name , ext;
            $.each( e.target.files , function ( i , v ) {
                data.append( e.target.id + '[]' , v );
            } );
            data.append( 'dir' , 'complain' );
            data.append( 'name' , e.target.id );
            axios.post( url , data , {
                emulateJSON : true
            } ).then( function ( res ) {
                if ( res.status == 200 ) {
                    $.each( res.data[ e.target.id ] , function ( i , v ) {
                        _this[ e.target.id ].push( v );
                    } );
                    $.each( _this[ e.target.id ] , function ( i , v ) {
                        name = v.split( '/' )[ 1 ].split( '.' )[ 0 ];
                        ext = v.split( '/' )[ 1 ].split( '.' )[ 1 ];
                        html += "<div style=\"position: relative;\" class='form-group' id='" + name + "' >";
                        html += "<img src='" +oss_url + v + " />";
                        html += "<div  class='img_remove_a' onclick='removeNodes(\"" + name + "\",\"" + ext + "\",\"" + e.target.id + "\")'></div>";
                        html += "</div>";
                    } )
                    $( '.' + e.target.id ).html( html );
                }
                _this.loading = true;
            } , function ( res ) {

            } );
        } ,
        // 订单投诉
        complain : function () {
            var data , url = '/userweb/My/complains';
            data = {
                supplier_id : this.supplier_id ,
                order_id : this.order_id ,
                content : this.complain_content ,
                image : this.complain_img ,
                tel : this.complain_tel
            };
            axios.post( url , data , {
                emulateJSON : true
            } ).then( function ( res ) {
                if ( res.data.status == 200 ) {
                    layer.msg( res.data.msg ,{
                        skin:'winning-class',
                        time:3000
                    } , function () {
                        window.location.reload();
                    } );
                }
            } ), function ( res ) {

            }
        }
    },

    mounted: function () {
        this.user_id        = localStorage.getItem('user_id');
        this.user_name      = localStorage.getItem('user_name');
        this.user_image     = localStorage.getItem('user_image');
        this.city           = localStorage.getItem('city');
        this.web_token      = localStorage.getItem('web_token');
        if (!this.user_id ){
            location.href = "/userweb/index/login";
        }
        this.ini();
    }
});

//删除图片
function removeNodes( name , ext , id ) {
    vm.loading = true;
    var path = 'product/' + name + '.' + ext , num = $.inArray( path , vm[ id ] );
    var data = { path : path } , url = '/apishop/Product/delImg';
    $.ajax( {
        url : url ,
        dataType : 'json' ,
        type : 'POST' ,
        data : data ,
        success : function ( res ) {
            if ( res.status == 200 ) {
                vm[ id ].splice( num , 1 );
                $( "#" + name ).remove();
                $( "#" + id ).val( '' );
            }
            vm.loading = false;
        } ,
        error : function () {
            layer.msg( '网络错误！' ,{
                skin:'winning-class',
                time:3000
            });
        }
    } );


}